package com.example.academic_affairs_system.controller;

import com.example.academic_affairs_system.common.R;
import com.example.academic_affairs_system.common.UserAuthorizationInfo;
import com.example.academic_affairs_system.model.dto.PageDTO;
import com.example.academic_affairs_system.model.dto.studentCourses.StudentCoursesPageDTO;
import com.example.academic_affairs_system.model.dto.user.EditPasswordDTO;
import com.example.academic_affairs_system.model.dto.user.LoginDTO;
import com.example.academic_affairs_system.model.dto.user.RestPasswordDTO;
import com.example.academic_affairs_system.model.entity.AasRole;
import com.example.academic_affairs_system.service.IAasClassService;
import com.example.academic_affairs_system.service.IAasRoleService;
import com.example.academic_affairs_system.service.IAasStudentCoursesService;
import com.example.academic_affairs_system.service.IAasUserService;
import com.github.benmanes.caffeine.cache.Cache;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Map;

/**
 * 公共接口
 *
 * @author hjx
 * @since 2023/02/03 10:39 PM
 */
@RestController
@RequestMapping("/common")
public class CommonController {
  @Resource IAasClassService iAasClassService;
  @Resource IAasUserService iAasUserService;
  @Resource IAasRoleService iAasRoleService;
  @Resource IAasStudentCoursesService iAasStudentCoursesService;

  @Resource(name = "userAuthorizationCache")
  Cache<UserAuthorizationInfo, Long> userAuthorizationCache;

  @Resource HttpServletRequest request;

  /**
   * 获取路由权限
   *
   * @return {@link Boolean} 如果为 true 则开放所有路由，否则就是指定特定路由
   */
  @GetMapping("/roles")
  public R getRoutersRole() {
    String token = request.getHeader(HttpHeaders.AUTHORIZATION);
    for (Map.Entry<UserAuthorizationInfo, Long> entry : userAuthorizationCache.asMap().entrySet()) {
      UserAuthorizationInfo key = entry.getKey();
      // 找到用户令牌，返回是否为负责人
      if (key.getToken().equals(token)) {
        Long userId = key.getUserId();
        AasRole roleByUserId = iAasRoleService.getRoleByUserId(userId);
        return R.ok(roleByUserId.getUrls().equals("*"));
      }
    }
    return R.fail();
  }

  @GetMapping("/classlist")
  // FIXME：任意角色获取自己所在的班级
  public R directorList() {
    return R.ok(iAasClassService.getClassByCurrentUser());
  }

  @GetMapping("/courseslist")
  public R coursesList(StudentCoursesPageDTO dto) {
    return R.ok(iAasStudentCoursesService.getStudentCourses(dto));
  }

  @GetMapping("/logout")
  public R resetPwd() {
    iAasUserService.logout();
    return R.ok("退出成功", null);
  }

  @PostMapping("/resetPwd")
  public R resetPwd(@RequestBody @Valid RestPasswordDTO dto) {
    return R.ok(iAasUserService.resetPassword(dto));
  }

  @PostMapping("/editPwd")
  public R editPwd(@RequestBody @Valid EditPasswordDTO dto) {
    return R.ok(iAasUserService.editPassword(dto));
  }

  @PostMapping("/login")
  public void login(@RequestBody LoginDTO dto) {
    iAasUserService.login(dto);
  }
}
